package com.example.admin.service.imp;

import com.alibaba.fastjson.JSONObject;
import com.example.admin.Dto.PendingTaskDto;
import com.example.admin.Vo.PendingTaskVO;
import com.example.admin.base.PageResponse;
import com.example.admin.bean.LoginUserDetails;
import com.example.admin.constants.PostConstants;
import com.example.admin.service.Applicationservice;
import com.example.admin.utils.StringUtils;
import com.example.admin.utils.UserThreadLocal;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ImpApplication implements Applicationservice {
    @Autowired
    HistoryService historyService;

    @Override
    public PageResponse<PendingTaskVO> selectByPage(PendingTaskDto pendingTaskDto) {
        HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
        String subject = UserThreadLocal.getSubject();
        LoginUserDetails loginUserDetails = JSONObject.parseObject(subject, LoginUserDetails.class);


        if (loginUserDetails.getDeptNo().equals(PostConstants.YLGU_POST)){
            historicTaskInstanceQuery.taskNameLike("%-1");
            historicTaskInstanceQuery.taskAssignee(loginUserDetails.getId().toString());
        }
        if (loginUserDetails.getDeptNo().equals(PostConstants.HLZG_POST)){
            historicTaskInstanceQuery.taskNameLike("%-3");
        }
        if (StringUtils.isNotEmpty(pendingTaskDto.getCode())){
            historicTaskInstanceQuery.processVariableValueLike("code", pendingTaskDto.getCode());
        }

        if (!ObjectUtils.isEmpty(pendingTaskDto.getStartTime()) && !ObjectUtils.isEmpty(pendingTaskDto.getEndTime())){
            historicTaskInstanceQuery.taskCompletedAfter(pendingTaskDto.getStartTime()).taskCompletedBefore(pendingTaskDto.getEndTime());
        } else if (ObjectUtils.isEmpty(pendingTaskDto.getStartTime()) && !ObjectUtils.isEmpty(pendingTaskDto.getEndTime())) {
            historicTaskInstanceQuery.taskCompletedBefore(pendingTaskDto.getEndTime());
        } else if (!ObjectUtils.isEmpty(pendingTaskDto.getStartTime()) && ObjectUtils.isEmpty(pendingTaskDto.getEndTime())) {
            historicTaskInstanceQuery.taskCompletedAfter(pendingTaskDto.getStartTime());
        }

        if (!ObjectUtils.isEmpty(pendingTaskDto.getStatus())){
            historicTaskInstanceQuery.processVariableValueEquals("status",pendingTaskDto.getStatus());
        }

        if (!ObjectUtils.isEmpty(pendingTaskDto.getType())){
            historicTaskInstanceQuery.processVariableValueEquals("type",pendingTaskDto.getType());
        }

        List<HistoricTaskInstance> list = historicTaskInstanceQuery.includeProcessVariables().listPage((pendingTaskDto.getPagenum() - 1) * pendingTaskDto.getPagesize(), pendingTaskDto.getPagesize());
        ArrayList<PendingTaskVO> arrays = new ArrayList<>();

        for (HistoricTaskInstance historicTaskInstance : list) {
            PendingTaskVO vo = new PendingTaskVO();
            vo.setId(historicTaskInstance.getId());
            vo.setCheckInId(historicTaskInstance.getProcessVariables().get("checkInId").toString());
            vo.setApplicat(historicTaskInstance.getProcessVariables().get("applicat").toString());
            vo.setApplicationTime(historicTaskInstance.getCreateTime());
            vo.setCode(historicTaskInstance.getProcessVariables().get("code").toString());
            vo.setStatus(Integer.parseInt(historicTaskInstance.getProcessVariables().get("status").toString()));
            vo.setAssigneeId(historicTaskInstance.getAssignee());
            vo.setTitle(historicTaskInstance.getProcessVariables().get("title").toString());
            vo.setType(Integer.parseInt(historicTaskInstance.getProcessVariables().get("type").toString()));
//            加入数据到arrays
            arrays.add(vo);
        }
        long counts = historicTaskInstanceQuery.count();
//        一共有counts条数据，每页的大小是pendingTaskDto.getPagesize()，求一共有多少页
        long pages = counts % pendingTaskDto.getPagesize() == 0 ? counts / pendingTaskDto.getPagesize() : counts / pendingTaskDto.getPagesize() + 1;
        PageResponse<PendingTaskVO> of = PageResponse.of(arrays, pendingTaskDto.getPagenum(), pendingTaskDto.getPagesize(), pages, counts);
        return of;
    }
}
